<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>deploy列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/js/xkube.js?v=1" charset="utf-8"></script>
    <script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>

<style type="text/css">
  .layui-table-cell {
    height: auto;
    line-height: 15px !important;
    text-overflow: inherit;
    overflow: ellipsis;
    white-space: normal;
  }
  .layui-table-cell .layui-table-tool-panel li {
    word-break: break-word;
  }
</style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">nameSpace</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nameSpace" autocomplete="off" lay-verify="required" lay-reqtext="不能为空" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>

                        <div class="layui-inline" style="position: absolute;right:50px">
                            <label class="layui-form-label">当前集群</label>
                            <div class="layui-input-inline">
                                <select name="clusterId" lay-filter="cluster_Id" lay-search="" id="cluster_Id">
                                  <option value="">请选择集群</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="createNamespace"><i class="layui-icon">&#xe61f;</i>创建命名空间</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="yamlCreateNamespace"><i class="layui-icon">&#xe61f;</i>yaml创建命名空间</button>
            </div>
        </script>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">yaml编辑</a>
            <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="resLimit">默认资源限制</a>
            <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
        </script>
    </div>
</div>
</body>
<script type="text/javascript">
    	$(document).ready(function(){
          GetCurrClusterId();  
    	});		
</script>
<script type="text/html" id="TagTpl">
    {{# layui.each(d.labels.split(','), function(index, item){ }}
        <span>{{ item }}</span><br>
    {{# });  }}
</script>	
<script type="text/html" id="statusTpl">
  {{# if (d.status == 'Active') { }}
     <span style="color:#218868">{{ d.status}}</span>
	{{# } else { }}
    <span style="color:#FF5722">{{ d.status}}</span>
{{# } }}
</script>
<script>
//设置默认集群
SetDefaultCluster();
var clusterId = getQueryString("clusterId");
if (clusterId == null) {
	clusterId = getCookie("clusterId")
}
    layui.use(['form', 'table','miniTab'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
            miniTab = layui.miniTab,
            miniTab.listen();
            
            form.render('select');

        //选择集群
        form.on('select(cluster_Id)', function(data){
          //console.log(data.elem); //得到select原始DOM对象
          console.log(data.value); //得到被选中的值
          setCookie('clusterId',data.value,30);
          window.location.reload();
          //console.log(data.othis); //得到美化后的DOM对象
        });

        table.render({
            elem: '#currentTableId',
            url: '/xkube/ns/v1/List?clusterId='+clusterId,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData: function(res) { //实现加载全部数据后再分页
              if(res.code != 0){
                return {
               	  "code": res.code,
              	  "msg": res.msg,                 
                }
              }
            	if(this.page.curr) {
            		result=res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
            	}else{
            	  result=res.data.slice(0,this.limit);
              }
              return {
              	"code": res.code,
              	"msg":'',
              	"count":res.count,
              	"data":result
              };
            },
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'nameSpace', width: 200,title: '命名空间', sort: true},
                {field: 'labels', title: '标签', sort: true},
                {field: 'status', title: '状态', sort: true,templet: '#statusTpl'},
                {field: 'createTime', width:180, title: '创建时间'},
                {title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center"}
            ]],
            //size:'lg',
            limits: [25, 50, 100],
            limit: 25,
            page: true
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'createNamespace') {  // 监听添加操作
                var index = layer.open({
                    title: '创建',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '90%'],
                    content: '/page/xkube/namespaceCreate.html?clusterId='+clusterId,
                    end: function(){
                    	//window.parent.location.reload();//关闭open打开的页面时，刷新父页面
                      window.location.reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'yamlCreateNamespace') {  // 监听删除操作
                var index = layer.open({
                    title: '创建',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '85%'],
                    content: '/page/xkube/create_by_yaml.html',
                    end: function(){
                    	//window.parent.location.reload();//关闭open打开的页面时，刷新父页面
                      window.location.reload();//关闭open打开的页面时，刷新父页面
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } 
        });
 
       table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['45%', '92%'],
                    content: '/page/xkube/namespaceYaml.html?clusterId='+clusterId+'&nameSpace='+data.nameSpace,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'resLimit') {
                var index = layer.open({
                    title: '资源限额',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['55%', '70%'],
                    content: '/page/xkube/namespaceResLimit.html?clusterId='+clusterId+'&nameSpace='+data.nameSpace,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('警告:删除命名空间会将命名空间下的资源都删除，确定删除'+data.nameSpace+'?', {icon: 2, title:'删除提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('此处需运行1-2s左右');
                			 $.ajax({
                			    url: "/xkube/ns/v1/Del?clusterId="+clusterId+'&nameSpace='+data.nameSpace,
                			    type: "get",
                			    //data: data,
                			    //dataType: "json",
                			    success: function (resp) {
                            layer.close(index2);
                						//console.log(resp);
                						 if(resp.code == 0){
                							 layer.msg('删除成功', {icon: 1,time:2000});
                							 window.location.reload();
                							 //obj.del();
                						 }else{
                							 layer.msg(resp.msg,{icon:2});
                						 }
                				  }
                		   });		  	  
                },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    });                
            }
        });
    });
</script>
</html>